package cn.mugutu.finance.query;

import cn.mugutu.finance.domain.DoubleBall;
import cn.mugutu.finance.enums.DoubleBallNumberType;
import lombok.Data;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;


@Data
public class DoubleBallQuery extends BaseQuery {

    @DateTimeFormat(pattern = "yyyy-MM-dd") //接收前台传递过来的日期参数
    private Date startDate;    //开始时间
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endDate;      //结束时间
    private Integer startPeriod;    //开始期数
    private Integer endPeriod;      //结束期数

    public DoubleBallQuery() {
        super(DoubleBall.class);
    }

    @Override
    public void addWhere() {
        if (startPeriod != null) {
            addWhere(" o.period >= ? ", startPeriod);
        }
        if (endPeriod != null) {
            addWhere(" o.period <= ? ", endPeriod);
        }
        if (startDate != null) {
            addWhere(" o.openAwardDate >= ? ", startDate);
        }
        if (endDate != null) {
            endDate = DateUtils.addDays(endDate, 1);
            addWhere(" o.openAwardDate <= ? ", endDate);
        }

        addWhere(" o.type = ? ", DoubleBallNumberType.ACTUAL);

        //按期数倒序排列
        setSort("period");
        setOrder("desc");
    }

}
